import sys
import virtualenv, textwrap

output = virtualenv.create_bootstrap_script(textwrap.dedent(r"""
import os, subprocess
import sys

def query_yes_no(question, default="yes"):
    valid = {"yes":True,   "y":True,  "ye":True,
             "no":False,     "n":False}
    if default == None:
        prompt = " [y/n] "
    elif default == "yes":
        prompt = " [Y/n] "
    elif default == "no":
        prompt = " [y/N] "
    else:
        raise ValueError("invalid default answer: '%s'" % default)

    while True:
        sys.stdout.write(question + prompt)
        choice = raw_input().lower()
        if default is not None and choice == '':
            return valid[default]
        elif choice in valid:
            return valid[choice]
        else:
            sys.stdout.write("Please respond with 'yes' or 'no' "
                             "(or 'y' or 'n').\n")

def after_install(options, home_dir):
    trunk = join(home_dir, 'trunk')
    svn_url = 'https://django-example-photogallery.googlecode.com/svn/trunk/'
    if not os.path.exists(trunk):
        print 'Checkouts django-example-photogallery from %s to %s' % (svn_url, trunk)
        subprocess.call(['svn', 'co', svn_url, trunk])
    else:
        print 'Abort checkouting of django-example-photogallery since "trunk/" folder already exists'
        
    if sys.platform == 'win32':
        bin = 'Scripts'
    else:
        bin = 'bin'

    pip = join(home_dir, 'bin', 'pip')
    req = join(trunk, 'requirements.txt')
    if not os.path.exists(req):
        print 'No requirements.txt in the trunk folder.'
    else:
        if query_yes_no(
            'For install PIL in virtualenv with JPEG and PNG support you need to:\n'
            '    1. Install libjpeg62-dev, libfreetype6-dev, zlib1g-dev\n'
            '    2. Create symlinks in /path/to/env/lib/ to the following files:\n'
            '        (a) /usr/lib/i386-linux-gnu/libjpeg.so\n'
            '        (b) /usr/lib/i386-linux-gnu/libfreetype.so\n'
            '        (c) /usr/lib/i386-linux-gnu/libz.so\n'
            'Did you?', default='no'):
            print 'Installing dependecies using pip...'
            subprocess.call([pip, 'install', '-E', home_dir, '-r', req])
        else:
            print 'Abort installing depencies'
"""))


f = open('gallery-bootstrap.py', 'w').write(output)
